﻿Imports SmartStats.correlation
Public Class Regression
    Public Function SimpleLinearRegression(ByVal in1() As Double, ByVal in2() As Double) As Double()
        Dim reg_params(1) As Double
        Dim objDispersion As New dispersion
        Dim objCorrelation As New correlation
        reg_params(0) = objCorrelation.covariance(in1, in2) / objDispersion.Variance(in1)
        reg_params(1) = in2.Average - reg_params(0) * in1.Average
        Return (reg_params)
    End Function


    Public Function MultipleLinearRegression(ByVal in1(,) As Double, ByVal in2() As Double) As ArrayList
        Dim objMatOps As New MatrixOperations
        Dim xTranspose(,) As Double = objMatOps.matrixTranspose(in1)

        Dim inverseMat(,) As Double = objMatOps.matrixInvert(objMatOps.matrixMultiplication(xTranspose, in1))

        'objDataset.
        Dim intermediateMatrix(,) As Double = objMatOps.matrixMultiplication(inverseMat, xTranspose)
        Dim betaCoeff(,) As Double
        betaCoeff = objMatOps.matrixVectorMultiplication(intermediateMatrix, in2)

        Dim returnList As New ArrayList
        Dim predicted_val(,) As Double = objMatOps.matrixMultiplication(in1, betaCoeff)
        Dim errorVal(predicted_val.GetLength(0) - 1) As Double
        Dim charted_val(predicted_val.GetLength(0) - 1) As Double

        For i As Integer = 0 To charted_val.GetLength(0) - 1
            charted_val(i) = predicted_val(i, 0)
            errorVal(i) = charted_val(i) - in2(i)
        Next
        returnList.Add(betaCoeff)
        returnList.Add(charted_val)
        returnList.Add(errorVal)
        Return returnList
    End Function

    Public Function ResponseSurfaceAnalysis(ByVal X(,) As Double, ByVal Y() As Double, ByVal schema As Integer) As ArrayList
        Dim objMatOps As New MatrixOperations
        Dim sizeRegressionBaseY = (objMatOps.factorial(schema - 1) / (2 * objMatOps.factorial(schema - 1 - 2))) + 2 * (schema - 1) + 1  'oNE CONSTANT , ONE LINER, ONE SQUARED, AND COMBINATION fact(n)/fact(2)*fact(n-2)  combination schema consists one o/p also thats y -1 factor

        Dim regressionBase(X.GetLength(0) - 1, sizeRegressionBaseY - 1) As Double

        'MsgBox(xTranspose.GetLength(0) & "  " & xTranspose.GetLength(1))

        For i As Integer = 0 To regressionBase.GetLength(0) - 1
            Dim xindex As Integer = 0
            Dim yindex As Integer = 1
            For j As Integer = 0 To regressionBase.GetLength(1) - 1
                If j = 0 Then ' for constant term
                    regressionBase(i, j) = 1
                ElseIf j <= schema - 1 And j >= 0 Then 'schema.GetLength(0) - 1 for constant term
                    regressionBase(i, j) = X(i, j - 1) 'X(i,j-1) for constant term
                ElseIf j <= 2 * (schema - 1) And j > schema - 1 Then 'schema.GetLength(0) - 1 and 2 * (schema.GetLength(0) - 1) for  constant term
                    regressionBase(i, j) = (X(i, j - (schema - 1) - 1)) ^ 2 '(X(i, j - (schema.GetLength(0) - 1)-1))for  constant term
                ElseIf j > 2 * (schema - 1) Then '(2 * (schema.GetLength(0) - 1)) for constant term 
                    regressionBase(i, j) = X(i, xindex) * X(i, yindex)
                    yindex = yindex + 1
                    If yindex >= schema - 1 Then
                        xindex = xindex + 1
                        yindex = xindex + 1
                    End If
                End If
            Next
        Next


        Dim regressionBaseTranspose(,) As Double = objMatOps.matrixTranspose(regressionBase)

        Dim inverseMat(,) As Double = objMatOps.matrixInvert(objMatOps.matrixMultiplication(regressionBaseTranspose, regressionBase))

        'objDataset.
        Dim intermediateMatrix(,) As Double = objMatOps.matrixMultiplication(inverseMat, regressionBaseTranspose)
        Dim betaCoeff(,) As Double
        betaCoeff = objMatOps.matrixVectorMultiplication(intermediateMatrix, Y)
        Dim returnList As New ArrayList
        Dim predicted_val(,) As Double = objMatOps.matrixMultiplication(regressionBase, betaCoeff)
        Dim errorVal(predicted_val.GetLength(0) - 1) As Double
        Dim charted_val(predicted_val.GetLength(0) - 1) As Double

        For i As Integer = 0 To charted_val.GetLength(0) - 1
            charted_val(i) = predicted_val(i, 0)
            errorVal(i) = charted_val(i) - Y(i)
        Next
        returnList.Add(betaCoeff)
        returnList.Add(charted_val)
        returnList.Add(errorVal)
        Return returnList
    End Function

    Public Function LinearCurveFit(ByVal in1() As Double, ByVal in2() As Double) As Double(,)
        Dim dependents(1) As Double
        Dim independents(1, 1) As Double


        Dim sum As Double = 0
        Dim sum1 As Double = 0
        For i As Integer = 0 To in1.GetLength(0) - 1
            sum = sum + in1(i) * in2(i)
            sum1 = sum1 + in1(i) ^ 2
        Next

        dependents(0) = in2.Average * in2.GetLength(0) 'sum of array
        dependents(1) = sum
        independents(0, 0) = in1.GetLength(0)
        independents(0, 1) = in1.Average * in1.GetLength(0)
        independents(1, 0) = in1.Average * in1.GetLength(0)
        independents(1, 1) = sum1

        Dim objMat As New MatrixOperations
        Dim curvefit(,) As Double = objMat.matrixVectorMultiplication(objMat.matrixInvert(independents), dependents)
        Return curvefit
    End Function

    Public Function ParabolicCurveFit(ByVal in1() As Double, ByVal in2() As Double) As Double(,)
        Dim dependents(2) As Double
        Dim independents(2, 2) As Double
        Dim sum As Double = 0
        Dim sum1 As Double = 0
        Dim sum2 As Double = 0
        Dim sum3 As Double = 0
        Dim sum4 As Double = 0

        For i As Integer = 0 To in1.GetLength(0) - 1
            sum = sum + in1(i) * in2(i)
            sum1 = sum1 + in1(i) ^ 2
            sum2 = sum2 + (in1(i) ^ 2) * in2(i)
            sum3 = sum3 + in1(i) ^ 3
            sum4 = sum4 + in1(i) ^ 4
        Next

        dependents(0) = in2.Average * in2.GetLength(0) 'sum of array
        dependents(1) = sum
        dependents(2) = sum2
        independents(0, 0) = in1.GetLength(0)
        independents(0, 1) = in1.Average * in1.GetLength(0)
        independents(0, 2) = sum1
        independents(1, 0) = in1.Average * in1.GetLength(0)
        independents(1, 1) = sum1
        independents(1, 2) = sum3
        independents(2, 0) = sum1
        independents(2, 1) = sum3
        independents(2, 2) = sum4

        Dim objMat As New MatrixOperations
        Dim curvefit(,) As Double = objMat.matrixVectorMultiplication(objMat.matrixInvert(independents), dependents)
        Return curvefit
    End Function

    Public Function PolynomialCurveFit(ByVal in1() As Double, ByVal in2() As Double, ByVal degree As Integer) As Double(,)
        Dim dependents(degree) As Double
        Dim independents(degree, degree) As Double

        For i As Integer = 0 To independents.getlength(0) - 1
            For j As Integer = 0 To independents.getlength(1) - 1
                Dim sum As Double = 0
                For k As Integer = 0 To in1.getlength(0) - 1
                    sum = sum + in1(k) ^ (i + j)
                Next
                independents(i, j) = sum
            Next
            Dim sum1 As Double = 0
            For l As Integer = 0 To in1.getlength(0) - 1
                sum1 = sum1 + in2(l) * in1(l) ^ i
            Next
            dependents(i) = sum1
        Next

        Dim objMat As New MatrixOperations
        Dim curvefit(,) As Double = objMat.matrixVectorMultiplication(objMat.matrixInvert(independents), dependents)
        Return curvefit
    End Function
End Class
